package lib

import (
	"GameServer/Common/data"
	"GameServer/Common/utils"
	"GameServer/Game/errors"
	"GameServer/Game/m"
	"encoding/json"
)

func GetClientCredentials() (r *m.ClientCredReqMsg, err error) {
	// 获取客户申请客户信息
	// r = make(map[string]string)
	// m["grant_type"] = "client_credentials"
	// m["client_id"] = "102428377"
	// m["client_secret"] = "a7ed6b590d73c0f388eb13ff690950948262d60960d6465a880aea9ef82606f1"
	b, err := utils.ReadJsonFile("SystemInfos.json")
	if err != nil {
		return
	}
	r = &m.ClientCredReqMsg{}
	err = json.Unmarshal(b, r)
	if err != nil {
		return
	}
	return
}

// 修改玩家的token
func SetUserPush(uid string, pushToken string, platform int64) (err error) {
	up := m.GetUserPush(uid)
	up.PushToken = pushToken
	up.Platform = platform
	up.UpdateTime = utils.TNow().Unix()

	up.Update(up)
	return
}

// 修改玩家的token
func GetUserPush(uid string) (r *m.UserPush, err error) {
	r = m.GetUserPush(uid)
	return
}

// 获取推送的消息
func GetPushMsg(id int64, langsTyp string) (title string, body string, err error) {
	tpl, ok := data.Get("PushMsgTypes", uint64(id))
	if !ok {
		err = errors.NewErrcode(data.Errcode.ConfigNotFound, "没有找到推送消息配置表,PushMsgTypes")
		return
	}
	tplT := tpl.(data.PushMsgType)
	tplTit, ok := data.Get("PushMsgLangs", uint64(tplT.TitleId))
	if !ok {
		err = errors.NewErrcode(data.Errcode.ConfigNotFound, "没有找到语言配置表,PushMsgLangs")
		return
	}
	tplBod, ok := data.Get("PushMsgLangs", uint64(tplT.BodyId))
	if !ok {
		err = errors.NewErrcode(data.Errcode.ConfigNotFound, "没有找到语言配置表,PushMsgLangs")
		return
	}
	tplTitT := tplTit.(data.PushMsgLang)
	tplBodT := tplBod.(data.PushMsgLang)
	switch langsTyp {
	case "en": // 英文
		return tplTitT.En, tplBodT.En, nil
	case "zh": // 简体中文
		return tplTitT.Cn, tplBodT.Cn, nil
	case "tc": // 繁体中文
		return tplTitT.Tracn, tplBodT.Tracn, nil
	}
	return tplTitT.En, tplBodT.En, nil
}
